package com.wondersgroup.fjzlpt.org.service.impl.jgqy.fwgl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.quick.framework.exception.ServiceException;
import com.quick.framework.util.LoggerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.fwgl.FwglDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.fwgl.FwglYljgDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.fwgl.LyjlDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.fwgl.TljlDAO;
import com.wondersgroup.fjzlpt.org.service.inter.jgqy.fwgl.FwglService;


@Service
public class FwglServiceImpl implements FwglService{

	@Autowired
	private FwglDAO fwglDAO;
	
	@Autowired
	private LyjlDAO lyjlDAO;
	
	@Autowired
	private TljlDAO tljlDAO;
	
	@Autowired
	private FwglYljgDAO fwglYljgDAO;
	
	
	
	/**
	 * 保存
	 * @param map
	 * @return
	 */
	public JSONObject save(Map map) {
		//时间数据：转换为Date
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String YQFWSJ = map.get("YQFWSJ").toString();
			if(!StringUtils.isEmpty(YQFWSJ)){
					map.put("YQFWSJ", sdf.parse(YQFWSJ));
			}
			String TLSJ = map.get("TLSJ").toString();
			if(!StringUtils.isEmpty(YQFWSJ)){
					map.put("TLSJ", sdf.parse(TLSJ));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		ResultObject ro = new ResultObject();
		String method = map.get("method").toString();
		if("add".equals(method)){
			insert(map, ro);
		}
		else if("update".equals(method)){
			update(map, ro);
		}
		else if("fwly".equals(method)){
			fwly(map, ro);
		}
		else if("fwtl".equals(method)){
			fwtl(map, ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	/**
	 * 新增
	 * @param map
	 * @param ro
	 */
	private void insert(Map<String, String> map, ResultObject ro){
		try {
			String fwglid = UUID.randomUUID().toString().toUpperCase();
			map.put("FWGLID", fwglid);
			map.put("SFSX", "0");
			fwglDAO.insert(map);
			/**创建关联表插入的多条记录*/
			String yljgids = map.get("YLJGIDS");
			String[] ids = yljgids.split(",");
			List<Map> list = new ArrayList<Map>();
			for (String str : ids) {
				Map  ma = new HashMap();
				ma.put("FWGLID", fwglid);
				ma.put("YLJGID", str);
				ma.put("FWZT", "01");
				list.add(ma);
			}
			fwglYljgDAO.insertBatch(list);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000501","新增签约申请失败！");
		}
	}
	
	/**
	 * 更新
	 * @param map
	 * @param ro
	 */
	private void update(Map<String, String> map, ResultObject ro){
		try {
			String fwglid = map.get("FWGLID");
			fwglDAO.updateForPrimaryKey(map);
			/**根据服务管理id删除记录*/
			fwglYljgDAO.delete(map);
			/**创建关联表插入的多条记录*/
			String yljgids = map.get("YLJGIDS");
			String[] ids = yljgids.split(",");
			List<Map> list = new ArrayList<Map>();
			for (String str : ids) {
				Map  ma = new HashMap();
				ma.put("FWGLID", fwglid);
				ma.put("YLJGID", str);
				ma.put("FWZT", "01");
				list.add(ma);
			}
			fwglYljgDAO.insertBatch(list);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000502","更新签约申请失败！");
		}
	}

	
	/**
	 * 服务履约
	 * @param map
	 * @param ro
	 */
	private void fwly(Map<String, String> map, ResultObject ro){
		try {
			map.put("SFSX", "1");
			fwglDAO.updateForPrimaryKey(map);
			lyjlDAO.insert(map);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000503","新增服务履约失败！");
		}
	}
	
	/**
	 * 服务讨论
	 * @param map
	 * @param ro
	 */
	private void fwtl(Map<String, String> map, ResultObject ro){
		try {
			map.put("SFSX", "1");
			fwglDAO.updateForPrimaryKey(map);
			map.put("TLID", UUID.randomUUID().toString().toUpperCase());
			tljlDAO.insert(map);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000504","新增服务讨论失败！");
		}
	}
	
	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	public JSONObject queryPage(Map<String, String> map) {
		return fwglDAO.queryPage(map);
	}

	/**
	 * 根据条件查询所有数据
	 * @param map
	 * @return
	 */
	public JSONArray queryList(Map<String, String> map) {
		JSONArray jsary = new JSONArray();
		try {
			jsary = fwglDAO.queryAll(map);
			
			for(int i=0;i<jsary.size();i++){
				JSONObject item = jsary.getJSONObject(i);
				String SFSX = item.getString("SFSX");
				if("0".equals(SFSX)){
					item.put("SFSX", "已新建");
				}else{
					item.put("SFSX", "已服务");
				}
			}
			
			JSONObject json = new JSONObject();
			json.put("FWMC", "服务名称");
			json.put("TGFWJGMC", "提供服务机构");
			json.put("YLJGMC", "接收服务机构");
			json.put("YLTLXMC", "医联体类型");
			json.put("YQFWSJ", "预期服务时间");
			json.put("FWNR", "服务内容");
			json.put("SFSX", "状态");
			jsary.add(0,json);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000505","根据id查询服务失败！");
		}
		return jsary;
	}

}
